package cz.acies.data.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cz.acies.data.DBConnector;
import cz.acies.data.persist.ModelObject;
import cz.acies.uniql.sql.SelectQuery;
import cz.acies.uniql.sql.element.UniQLCondition;
import cz.acies.uniql.sql.element.UniQLWhere;

public class DefaultDAO extends AbstractDAO
{
	protected static final String UNKNOW = "";
	
	protected QueryRunner qr;
	
	public List<ModelObject> selectAll(String tableName, Class c) {
		List<ModelObject> data = new ArrayList<ModelObject>();
		try {
			try {
				//QueryRunner qr = new QueryRunner(ds());
				SelectQuery sql = new SelectQuery(dbmd()).addFrom(""+tableName);  
				ResultSetHandler<List<ModelObject>> handler = new BeanListHandler<ModelObject>(c);
				data = qr().query(sql.toSQLString(), handler);
			} catch (Exception exc) {
				exc.printStackTrace();
			}
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return data;
	}
	
	public List<ModelObject> selectById(String tableName, String id, Class c) {
		List<ModelObject> data = new ArrayList<ModelObject>();
		try {
			//QueryRunner qr = new QueryRunner(ds());
			SelectQuery sql = new SelectQuery(dbmd()).addFrom(""+tableName)
				.add(new UniQLCondition(tableName+".id", "=", "'"+id+"'"));  
			ResultSetHandler<List<ModelObject>> handler = new BeanListHandler<ModelObject>(c);
			data = qr().query(sql.toSQLString(), handler);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return data;
	}

	public List<ModelObject> selectByWhere(UniQLWhere where, Class c) {
		/*
		List<ModelObject> data = new ArrayList<ModelObject>();
		try {
			QueryRunner qr = new QueryRunner(ds());
			SelectQuery sql = new SelectQuery(dbmd()).addFrom(""+this.tableName).addWhere(where);  
			ResultSetHandler<List<ModelObject>> handler = new BeanListHandler<ModelObject>(c);
			data = qr.query(sql.toSQLString(true), handler);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		*/
		return selectByWhere(this.tableName, where, c);
	}

	public List<ModelObject> selectByWhere(String tableName, UniQLWhere where, Class c) {
		List<ModelObject> data = new ArrayList<ModelObject>();
		try {
			//QueryRunner qr = new QueryRunner(ds());
			SelectQuery sql = new SelectQuery(dbmd()).addFrom(""+tableName).addWhere(where);  
			ResultSetHandler<List<ModelObject>> handler = new BeanListHandler<ModelObject>(c);
			data = qr().query(sql.toSQLString(), handler);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return data;
	}

	public boolean existsRecord(String table, String id) {
		try {
			SelectQuery sql = new SelectQuery(dbmd()).addFrom(table).add(new UniQLCondition(table+".id", "=", "'"+id+"'")); 
			//QueryRunner qr = new QueryRunner(DBConnector.instance().getDataSource());
			List<Map<String, Object>> list = qr().query(sql.toSQLString(), new DefaultMapListHandler(new DefaultRowProcessor()));
			return !list.isEmpty();  
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return false;
	}
	
	public boolean existsRecord(String table, UniQLWhere where) {
		try {
			SelectQuery sql = new SelectQuery(dbmd()).addFrom(table).addWhere(where); 
			//QueryRunner qr = new QueryRunner(DBConnector.instance().getDataSource());
			List<Map<String, Object>> list = qr().query(sql.toSQLString(), new DefaultMapListHandler(new DefaultRowProcessor()));
			return !list.isEmpty();  
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return false;
	}

	public boolean existsRecord(ModelObject obj) throws Exception {
		if (obj == null || !obj.hasId()) return false;
		String id = obj.getId();
		return super.existsRecord(id);
	}

	protected QueryRunner qr() {
		if (this.qr == null) {
			this.qr = new QueryRunner(ds());		
		}
		return qr;
	}
}
